package com.lonewalker.sparrow.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lonewalker.sparrow.annotation.AvoidReSubmit;
import com.lonewalker.sparrow.domain.ApiResult;
import com.lonewalker.sparrow.req.UserQueryReq;
import com.lonewalker.sparrow.req.UserReq;
import com.lonewalker.sparrow.service.UserService;
import com.lonewalker.sparrow.vo.SysUserVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息表 控制层
 *
 * @author lonewalker
 * @since 1.0
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    /**
     * 添加用户信息
     *
     * @param requestParam 请求参数
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @AvoidReSubmit(lockTime = 3)
    @PostMapping("/save")
    public ApiResult<Boolean> save(@RequestBody @Validated UserReq requestParam) {
        return ApiResult.successByData(userService.saveUser(requestParam));
    }

    /**
     * 根据主键删除用户信息
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("/remove/{id}")
    public ApiResult<Boolean> remove(@PathVariable Long id) {
        return ApiResult.successByData(userService.removeById(id));
    }

    /**
     * 根据主键更新用户信息
     *
     * @param requestParam 相关参数
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("/update")
    public ApiResult<Boolean> update(@RequestBody UserReq requestParam) {
        return ApiResult.successByData(userService.updateById(requestParam));
    }

    /**
     * 查询所有用户信息
     *
     * @return 所有数据
     */
    @GetMapping("/list")
    public ApiResult<List<SysUserVo>> list() {
        return ApiResult.successByData(userService.listUser());
    }

    /**
     * 分页查询用户信息
     *
     * @param requestParam 查询参数
     * @return 分页对象
     */
    @PostMapping("/page")
    public ApiResult<Page<SysUserVo>> page(@RequestBody @Validated UserQueryReq requestParam) {
        return ApiResult.successByData(userService.pageUser(requestParam));
    }

}
